﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pay.DTO {
    /// <summary>
    /// 返回响应消息： result_code是错误代码，默认1000成功，其它失败；result_msg是返回响应消息 data是业绩结果：可能无；
    /// </summary>
    [Serializable]
    public class result_base {
        /// <summary>
        /// 默认1000表示成功，其它失败
        /// </summary>
        public int result_code { get; set; }
        /// <summary>
        /// 返回执行结果消息
        /// </summary>
        public string result_msg { get; set; }
        /// <summary>
        /// 不参与签名
        /// </summary>
        public string sign { get; set; }
    }

    /// <summary>
    /// 返回带有LIST集合的响应消息： result_code是错误代码，默认1000成功，其它失败；result_msg是返回响应消息 data是业绩结果：可能无；
    /// </summary>
    [Serializable]
    public class result_data_base<T> : result_base {
        public result_data_base() { }
        public result_data_base(string result_msg) {
            this.result_msg = result_msg;
        }
        /// <summary>
        /// data是业绩结果：可能无
        /// </summary>
        public T data { get; set; }
    }

    /// <summary>
    /// 响应消息处理类
    /// </summary>
    public class t_ApiResultToJson {
        /// <summary>
        /// 成功返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static T Json<T>(string msg, T result) where T : result_base, new() {
            result.result_msg = msg;
            return result;
        }
        /// <summary>
        /// 成功返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="msg"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static async Task<T> JsonAsync<T>(string msg, T result) where T : result_base, new() {
            Task<T> t1 = Task.Factory.StartNew(() => {
                result.result_msg = msg;
                return result;
            });
            return await t1;
        }
        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static T Json<T>(string msg) where T : result_base, new() {
            return new T {
                result_msg = msg,
                result_code = 0
            };
        }
        /// <summary>
        /// 失败返回
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static async Task<T> JsonAsync<T>(string msg) where T : result_base, new() {
            Task<T> t1 = Task.Factory.StartNew(() => new T {
                result_msg = msg,
                result_code = 0
            });
            return await t1;
        }
    }

    /// <summary>
    /// 分页信息
    /// </summary>
    [Serializable]
    public class t_ApiPagedResult<T> : result_base {
        public t_ApiPagedResult() { }
        public t_ApiPagedResult(List<T> rows, int total, int page, int size) {
            this.rows = rows;
            this.total_result = total;
            this.page = page;
            this.size = size;
        }
        /// <summary>
        /// 当前页
        /// </summary>
        public int page { get; set; }
        /// <summary>
        /// 当页N条
        /// </summary>
        public int size { get; set; }
        /// <summary>
        /// 总页数
        /// </summary>
        public int total_pages {
            get {
                if (total_result % size == 0)
                    return total_result / size;
                return total_result / size + 1;
            }
        }
        /// <summary>
        /// 总记录数
        /// </summary>
        public int total_result { get; set; }

        /// <summary>
        /// 返回分页记录数据
        /// </summary>
        public List<T> rows { get; set; }
    }
}
